//
//  BindPhoneViewController.m
//  YongChuanTradingArea
//
//  Created by Vivian on 2017/9/11.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "BindPhoneViewController.h"

@interface BindPhoneViewController ()
@property (weak, nonatomic) IBOutlet UITextField *telTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *msgBtn;
/** 倒计时*/
@property (nonatomic, assign) NSInteger timeCount;

@end

@implementation BindPhoneViewController
{
    NSTimer * _timer;
}

- (void)dealloc{
    //让定时器暂停
    [_timer setFireDate:[NSDate distantFuture]];
    _timer = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //让定时器暂停
    [_timer setFireDate:[NSDate distantFuture]];
    [_timer invalidate];
    [self resetVerifyStatus];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"绑定手机";
    self.view.backgroundColor = kBgColor;
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - btn clicked
- (IBAction)bindBtnAction:(UIButton *)sender {
    if (![self.telTF.text isTelephoneNumber]) {
        [MBProgressHUD showError:@"请输入正确的手机号！"];
        return;
    }
    if (self.codeTF.text.length == 0) {
        [MBProgressHUD showError:@"请输入验证码！"];
        return;
    }
    [self requestData];
}

- (IBAction)msgBtnAction:(UIButton *)sender {
    if ([self.telTF.text isTelephoneNumber]) {
        [self getCode];
        [self resetVerifyStatus];
        sender.enabled = NO;
        // 定时器
        self.timeCount = 30;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
        [_timer setFireDate:[NSDate distantPast]];
    }else{
        [MBProgressHUD showError:@"请检查输入的手机号是否正确"];
    }
}

#pragma mark - 定时器
-(void)timer:(NSTimer *)timer
{
    self.timeCount --;
    if (self.timeCount > 0) {
        NSString * str = [NSString stringWithFormat:@"%lds",(long)self.timeCount];
        [self.msgBtn setTitle:str forState:UIControlStateNormal];
        [self.msgBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    }else{
        [self resetVerifyStatus];
    }
}

#pragma mark - 重设获取验证码状态
- (void)resetVerifyStatus{
    NSString * str = @"获取验证码";
    self.msgBtn.enabled = YES;
    [self.msgBtn setTitle:str forState:UIControlStateNormal];
    [self.msgBtn setTitleColor:kMainColor forState:UIControlStateNormal];
    //让定时器暂停
    [_timer setFireDate:[NSDate distantFuture]];
    [_timer invalidate];
}

#pragma mark - request data
- (void)getCode{
    [LBNetworkingInstance requestGET:getVerifyCode parameters:@{@"phoneNumber":self.telTF.text} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
    } failure:^(NSError *err) {
        
    }];
}

- (void)requestData{
    __weak typeof(self) weakSelf = self;
    [LBNetworkingInstance requestGET:modifyPassword parameters:@{@"memberId":[LBUserManage defaultManager].user_id?:@"",@"valiteCode":self.codeTF.text,@"updateInfo":self.telTF.text?:@"",@"type":@"3"} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        if (code == YCNetworkingStatusCodeSuccess){
            [LBUserManage defaultManager].userModel = [UserModel yy_modelWithJSON:responseObject[@"Data"]];
            [MBProgressHUD showSuccess:@"修改手机号码成功！"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError *err) {
        
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
